//
//  HJGroupNoticeController.m
//  KaiChat
//
//  Created by lx on 2023/11/15.
//

#import "HJGroupNoticeController.h"
#import "HJIM.h"
#import "HJChatMessageModel.h"
#import "HJGroupEditNoticeController.h"

@interface HJGroupNoticeController ()

@property (nonatomic, strong) UIScrollView *mainView;
@property (nonatomic, weak) UILabel *noticeLabel;
@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UILabel *nicknameLabel;
@property (nonatomic, weak) UILabel *dateLabel;
@property (nonatomic, weak) UIView *lineView;

@end

@implementation HJGroupNoticeController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    self.navigationBar.title = @"Group notice";
    
    if (self.groupModel.role.intValue == 1 || self.groupModel.role.intValue == 2) {
        UIButton *editButton = [UIButton addToView:self.navigationBar.contentView];
        [editButton setImage:[UIImage imageNamed:@"edit_icon"] forState:UIControlStateNormal];
        [editButton addTarget:self action:@selector(didClickEditButton) forControlEvents:UIControlEventTouchUpInside];
        [editButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.inset(leftRightMargin - 5);
            make.centerY.offset(0);
            make.width.height.offset(30);
        }];
    }
    
    UIView *bottomView = [UIView addToView:self.view];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(TabbarSafeBottomMargin + 38);
    }];
    
    UILabel *tipLabel = [UILabel lableWithSuperView:bottomView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:@"Only group owner and admin can edit" textAlignment:NSTextAlignmentLeft];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.centerX.offset(0);
    }];
    
    self.mainView = [UIScrollView addToView:self.view];
    self.mainView.frame = CGRectMake(0, KNavBarHight, KScreenWidth, KScreenHeight - KNavBarHight - 38 - TabbarSafeBottomMargin);
    self.mainView.backgroundColor = UIColor.tj_backgroundColor;
    CGSize contentSize = self.mainView.frame.size;
    
    self.mainView.contentSize = contentSize;
    self.mainView.showsVerticalScrollIndicator = NO;
    self.mainView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

    UIView *contentView = [[UIView alloc] init].addTo(self.mainView);
    contentView.backgroundColor = UIColor.tj_backgroundColor;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
        make.width.offset(contentSize.width);
        make.height.offset(contentSize.height);
    }];
    
    UIView *senderView = [UIView addToView:contentView];
    [senderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.inset(0);
        make.height.offset(82);
    }];
    
    UIImageView *avatarImageView = [UIImageView addToView:senderView];
    avatarImageView.addCornerRadius(20);
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.width.height.offset(40);
        make.centerY.offset(0);
    }];

    UILabel *nicknameLabel = [UILabel lableWithSuperView:senderView font:UIFontMake(12) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatarImageView).offset(4);
        make.left.equalTo(avatarImageView.mas_right).offset(10);
    }];
    
    UILabel *dateLabel = [UILabel lableWithSuperView:senderView font:UIFontMake(12) color:UIColor.tj_868391Color title:nil textAlignment:NSTextAlignmentLeft];
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nicknameLabel.mas_bottom).offset(6);
        make.left.equalTo(nicknameLabel);
    }];
    
    UIView *lineView = [UIView addToView:senderView];
    lineView.hidden = YES;
    lineView.backgroundColor = UIColor.tj_F3F2F7Color;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.height.offset(1);
        make.bottom.offset(0);
    }];
    
    UILabel *noticeLabel = [UILabel lableWithSuperView:contentView font:UIFontMake(12) color:UIColor.tj_868391Color title:nil textAlignment:NSTextAlignmentLeft];
    noticeLabel.numberOfLines = 0;
    [noticeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(senderView.mas_bottom).offset(16);
    }];

    _avatarImageView = avatarImageView;
    _dateLabel = dateLabel;
    _nicknameLabel = nicknameLabel;
    _noticeLabel = noticeLabel;
    _contentView = contentView;
    _lineView = lineView;
    
    [self loadData];
}

- (void)loadData {
    
    dispatch_async_sql_queue(^{
       
        HJChatGroupModel *groupModel = [HJIM.shareIM.groupManager queryGroupInfoByGroupId:self.groupModel.groupId];
        NSDictionary *extensionDict = groupModel.extension.mj_JSONObject;
        NSDictionary *groupNoticeDict = [extensionDict objectForKey:groupNoticeInfoKey];
        if (groupNoticeDict) {
            [self updateDataWithDict:groupNoticeDict content:groupModel.notice];
        }
        [HJIM.shareIM.groupManager loadGroupNoticeDetailByGroupId:self.groupModel.groupId completion:^(NSDictionary * _Nullable data) {
            if (groupNoticeDict) {
                if (![data[@"content"] isEqualToString:groupModel.notice] && [groupNoticeDict[@"publish_time"] integerValue] != [data[@"publish_time"] integerValue]) {
                    [self updateDataWithDict:data content:data[@"content"]];
                }
            } else {
                [self updateDataWithDict:data content:data[@"content"]];
            }
        }];
        
    });
    
}

- (void)updateDataWithDict:(NSDictionary *)groupNoticeDict content:(NSString *)content {
    dispatch_async_main_queue(^{
        self.lineView.hidden = NO;
        [self.avatarImageView tj_setImageWithUrl:groupNoticeDict[@"avatar"] placeholderImage:[UIImage imageNamed:defaultAvatarName]];
        self.nicknameLabel.text = groupNoticeDict[@"nickname"];
        self.dateLabel.text = [[NSDate dateWithTimeIntervalSince1970:[groupNoticeDict[@"publish_time"] integerValue]] stringWithFormat:@"dd MMM yyyy HH:mm"];
        self.noticeLabel.attributedText = [content titleMargin:3 alignment:NSTextAlignmentLeft];
        [self updateNoticeLabelHeight];
    });
}

 
- (void)updateNoticeLabelHeight {
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    
    CGSize contentSize = self.mainView.frame.size;
    
    if (_noticeLabel.tj_MaxY > contentSize.height) {
        CGSize size = CGSizeMake(contentSize.width, _noticeLabel.tj_MaxY);
        self.mainView.contentSize = size;
        [_contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.offset(0);
            make.width.offset(size.width);
            make.height.offset(size.height);
        }];
    }
}

- (void)didClickEditButton {
    HJGroupEditNoticeController *groupNoticeController = [[HJGroupEditNoticeController alloc] init];
    groupNoticeController.groupModel = self.groupModel;
    [groupNoticeController setEditComplete:^{
        self.noticeLabel.attributedText = [self.groupModel.notice titleMargin:3 alignment:NSTextAlignmentLeft];
        [self updateNoticeLabelHeight];
    }];
    [self.navigationController pushViewController:groupNoticeController];
}
 

@end
